<template>
  <div class="app-container">
    <div class="cardBox">
      <div style="display: flex" :gutter="10">
        <div style="flex: 1">
          <el-button
            @click="gobackWay"
            type="text"
            icon="el-icon-arrow-left"
            style="color: rgba(0, 0, 0, 0.85)"
            >返回</el-button
          >
          <el-divider direction="vertical"></el-divider>
          <span>抽奖活动商品列表</span>
        </div>
      </div>
    </div>

    <el-row
      class="cardBox"
      style="height: calc(85vh - 80px); margin-top: 20px; display: flex"
    >
      <el-col :span="11">
        <el-form :inline="true" class="search-form">
          <el-row style="display: flex; flex-wrap: wrap">
            <div style="flex: 1">
              <el-form-item label="商品名称">
                <el-input
                  placeholder="请输入商品名称"
                  v-model="UnselectedSearch.keywords"
                  maxlength="20"
                  show-word-limit
                  clearable
                >
                </el-input>
              </el-form-item>
              <el-form-item label="筛选条件">
                <!-- <el-input
                  placeholder="请输入商品名称"
                  v-model="UnselectedSearch.goods_pay_type"
                  maxlength="20"
                  show-word-limit
                  clearable
                >
                </el-input> -->
                <el-select
                  @change="getUnselectedListWay"
                  clearable
                  v-model="UnselectedSearch.goods_pay_type"
                  placeholder="请选择筛选条件"
                >
                  <el-option
                    v-for="item in [
                      { value: 1, label: '现金购买商品' },
                      { value: 2, label: '兑换商品' },
                    ]"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div>
              <el-form-item>
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-search"
                  @click="getUnselectedListWay"
                  >查询</el-button
                >
              </el-form-item>
            </div>
          </el-row>
        </el-form>
        <div class="marginTop">
          <el-table
            ref="getUnselectedRowKeys"
            @selection-change="UnselectedChange"
            :row-key="getUnselectedRowKeys"
            v-loading="UnselectedLoading"
            :data="UnselectedList"
            :header-cell-style="{
              'background-color': '#f3f8fe',
              color: '#606266',
              height: '50px',
            }"
            max-height="550"
          >
            <el-table-column :reserve-selection="true" type="selection">
            </el-table-column>
            <el-table-column align="center" prop="name" label="商品名称">
            </el-table-column>
            <el-table-column align="center" prop="date" label="所属供货商">
              <template slot-scope="scope">
                {{
                  scope.row.supplier ? scope.row.supplier.supplier_name : "-"
                }}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="date" label="库存">
              <template slot-scope="scope">
                {{ scope.row.product_sum_inventory }}
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              align="center"
              prop="date"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click="batchAssociationWay(scope.row)"
                  >关联商品</el-button
                >
              </template>
            </el-table-column>
            <template slot="empty">
              <el-empty description="请选择参与抽奖的商品"></el-empty>
            </template>
          </el-table>
        </div>
        <div style="text-align: right">
          <pagination
            :total="UnselectedTotal"
            :page.sync="UnselectedSearch.page"
            :limit.sync="UnselectedSearch.count"
            @pagination="getUnselectedListWay"
          />
        </div>
      </el-col>
      <el-col :span="2" style="position: relative">
        <div
          style="
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
          "
        >
          <el-button
            size="small"
            :loading="joinRemove"
            @click="batchAssociationWay"
            :disabled="selectUnList.length == 0"
            style="margin: 5px"
            type="primary"
            >批量关联</el-button
          >
        </div>
      </el-col>
      <el-col :span="11">
        <el-form :inline="true" class="search-form">
          <el-row style="display: flex; flex-wrap: wrap">
            <div style="flex: 1">
              <el-form-item label="商品名称">
                <el-input
                  placeholder="请输入商品名称"
                  v-model="selectedSearch.goods_name"
                  maxlength="20"
                  show-word-limit
                  clearable
                >
                </el-input>
              </el-form-item>
            </div>
            <div>
              <el-form-item>
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-search"
                  @click="getSelectedSListWay"
                  >查询</el-button
                >
              </el-form-item>
            </div>
          </el-row>
        </el-form>
        <div class="marginTop">
          <el-table
            ref="SelectedSList"
            v-loading="selectedLoading"
            highlight-current-row
            :data="SelectedSList"
            :header-cell-style="{
              'background-color': '#f3f8fe',
              color: '#606266',
              height: '50px',
            }"
            max-height="550"
          >
            <el-table-column align="center" prop="main_image" label="图片">
              <template slot-scope="scope">
                <div style="width: 40px; height: 40px; margin: 0 auto">
                  <el-image
                    v-if="scope.row.main_image"
                    :src="scope.row.main_image"
                    :preview-src-list="[scope.row.main_image]"
                  />
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="name" label="名称">
            </el-table-column>
            <el-table-column align="center" prop="date" label="操作">
              <template slot-scope="scope">
                <el-button
                  :loading="joinRemove"
                  type="danger"
                  size="mini"
                  @click="joinRemoveWay(scope.row)"
                  >移出商品</el-button
                >
              </template>
            </el-table-column>
            <template slot="empty">
              <el-empty description="请选择参与抽奖的商品"></el-empty>
            </template>
          </el-table>
          <div style="text-align: right">
            <pagination
              :total="selectedTotal"
              :page.sync="selectedSearch.page"
              :limit.sync="selectedSearch.count"
              @pagination="getSelectedSListWay"
            />
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  SaveAwardGoods,
  getAwardGoodsList,
  delAwardGoods,
} from "@/api/activity/index";
import { getPlatformGoodsList } from "@/api/index";
export default {
  data() {
    return {
      loading: false,
      UnselectedSearch: {
        keywords: "",
        goods_pay_type: "",
        page: 1,
        count: 10,
      },
      UnselectedList: [],
      UnselectedTotal: 0,
      UnselectedLoading: false,
      selectUnList: [],
      selectedSearch: {
        awardId: "",
        page: 1,
        count: 10,
        goods_name: "",
      },
      SelectedSList: [],
      selectedTotal: 0,
      selectedLoading: false,
      selectedList: [],

      joinRemove: false,
    };
  },
  created() {
    this.selectedSearch.awardId = this.$route.query.pId;
    this.getSelectedSListWay();
    this.getUnselectedListWay();
  },
  methods: {
    // 未选库
    getUnselectedListWay() {
      this.UnselectedLoading = true;
      getPlatformGoodsList(this.UnselectedSearch).then((res) => {
        this.UnselectedLoading = false;
        if (res.code !== 200 || res.status !== 1) {
          this.$messages.error(res.msg);
          return;
        }
        this.UnselectedList = res.data.list;
        this.UnselectedTotal = res.data.total;
      });
    },
    // 已选库
    getSelectedSListWay() {
      this.selectedLoading = true;
      getAwardGoodsList(this.selectedSearch).then((res) => {
        this.selectedLoading = false;
        if (res.code !== 200 || res.status !== 1) {
          return this.$messages.error(res.msg);
        }
        this.SelectedSList = res.data.list;
        this.selectedTotal = res.data.total;
      });
    },
    getUnselectedRowKeys(row) {
      return row.id;
    },
    UnselectedChange(val) {
      this.selectUnList = val;
    },
    batchAssociationWay(row) {
      let arr = [];
      this.joinRemove = true;
      if (row.id) {
        arr = [row.id];
      } else {
        this.selectUnList.forEach((item) => {
          arr.push(item.id);
        });
      }
      SaveAwardGoods({
        awardId: this.selectedSearch.awardId,
        goodsIds: arr.toString(),
      }).then((res) => {
        this.joinRemove = false;
        if (res.code !== 200 || res.status !== 1) {
          return this.$messages.error(res.msg);
        }
        this.$messages.success(res.msg);
        this.selectUnList = [];
        this.$refs.getUnselectedRowKeys.clearSelection();
        this.getSelectedSListWay();
        console.log(this.selectUnList);
      });
    },
    joinRemoveWay(row) {
      this.joinRemove = true;
      delAwardGoods({
        awardId: this.selectedSearch.awardId,
        goodsId: row.id,
      }).then((res) => {
        this.joinRemove = false;
        if (res.code !== 200 || res.status !== 1) {
          return this.$messages.error(res.msg);
        }
        this.$messages.success(res.msg);
        this.getSelectedSListWay();
      });
    },

    // 返回本地团商品列表
    gobackWay() {
      this.$router.push("/activity/raffle");
    },
  },
};
</script>

<style>
</style>